#coding:utf8

##from python 2.7

import os,sys
import re

field_pattern = re.compile(r'[\ ]{0,10}[a-z|A-Z]{1,20}[\ ]{1,20}[a-z|A-Z|0-9|\<|\>\_]{0,40}[\ ]{1,20}[a-z|A-Z|0-9|\_]{0,40}\;[\ ]{0,20}')
removed_pre_pattern = re.compile(r'[\ ]{0,10}\/\*[\ ]{0,20}[0-9|a-z|A-Z]{0,40}[\ ]{0,20}\*\/[\ ]{0,20}')

#单个文件输出
def change_file(file_path):
    if not os.path.isdir(file_path) and not os.path.isfile(file_path):
        print "file not found"
        return
    file_pointer = open(file_path)
    line = file_pointer.readline()
    while line:
        line = line.replace("\n","")
        output_file_content(line)
        line = file_pointer.readline()

    file_pointer.close()

#多个文件保存
def change_dir_sources(dir):
    if not os.path.isdir(dir):
        print "dir not found"

    out_dir = dir + 'data'
    is_exits = os.path.exists(out_dir)
    if not is_exits:
        os.makedirs(out_dir) 


    list = os.listdir(dir)#列出目录下的所有文件和目录
    for line in list:
        if line.find('.java') < 0:
            continue

        result = ''
        filepath = os.path.join(dir, line)
        result = get_change_file(filepath)
        out=file(get_new_file_full_name(filepath) ,"w")
        out.write(result)
        out.close()

    print 'finished'

def get_new_file_full_name(file_path):
    array = file_path.split('\\')
    return __file__.replace('app.py','') + "data\\" + array[len(array)-1]


def get_change_file(file_path):
    if not os.path.isdir(file_path) and not os.path.isfile(file_path):
        print "file not found"
        return
    file_pointer = open(file_path)
    line = file_pointer.readline()
    result = ''
    while line:
        line = removed_pre_pattern.sub('', line.replace("\n",""))
        result = result + get_file_content(line)
        line = file_pointer.readline()

    file_pointer.close()
    result = result + "}\n"
    return result

def output_file_content(line):
    if line.find("@FieldDoc") >= 0:
        line = line.replace('(\"', '|').replace('\")','|')
        array = line.split('|')
        print "/**"
        print "* " + array[1]
        print "*/"
        return
    if line.find("@JsonProperty") >= 0:
        print line
        return

    group = field_pattern.match(line)
    if group:
        print group.string.replace('\n','')
        return

def get_file_content(line):
    result = ''
    if line.find("public class") >= 0:
        result = result + line.replace("implements","").replace("SpecificRecord","") + "{\n"
        return result

    if line.find("@FieldDoc") >= 0:
        line = line.replace('(\"', '|').replace('\")','|')
        array = line.split('|')
        result = result + "/** \n"
        result = result + "* " + array[1] + "\n"
        result = result + "*/" + "\n"
        return result
    if line.find("@JsonProperty") >= 0:
        result = result + line +"\n"
        return result

    group = field_pattern.match(line)
    if group:
        result = result + group.string + "\n"
        result += build_get_set(group.string)
        return result

    return result

def build_get_set(line):
    array = line.replace(';','').split(' ')
    dataType = array[len(array)-2]
    beanName = array[len(array)-1]

    funcName = beanName[0].upper() + beanName[1:]

    beanName_lower = beanName[0].lower() + beanName[1:]
    lineStr = ''
    lineStr += "    public " + dataType + " get" + funcName + "() {\n return this." + beanName_lower + ";\n }\n"
    lineStr += "    public void set" + funcName + "(" + dataType + " " + beanName_lower + ") {\n this." + beanName_lower + " = " + beanName_lower + "; \n}\n"

    return lineStr



def test_output_file_content1():
    line = "public class ChOrderInfo implements SpecificRecord {"
    output_file_content(line)
    
def test_output_file_content2():
    line = '@FieldDoc("供应商ID不允许为空")'
    output_file_content(line)

def test_re_method():
    line = 'private String vendorId;'
    group = field_pattern.match(line)
    if group:
        print group.string

    line = 'private BigInter vendorId;'
    group = field_pattern.match(line)
    if group:
        print group.string


    line = 'private String driverVehicleTypeBrand;'
    group = field_pattern.match(line)
    if group:
        print group.string

    line = '@JsonProperty("VendorName")'
    group = field_pattern.match(line)
    if group:
        print group.string

def test_removed_pre_pattern():
    a = 'hello word'
    strinfo = re.compile('word')
    b = strinfo.sub('python',a)
    print b

def test_removed_pre_pattern_1():
    p = re.compile(r'[\ ]{0,10}\/\*[\ ]{0,20}[0-9|a-z|A-Z]{0,10}[\ ]{0,20}\*\/[\ ]{0,20}')

    a = '/*     */ '
    b = p.sub('',a)
    print b

    a = '  /*     */    iohoh'
    b = p.sub('',a)
    print b

    a = '  /*   123123  */    iohohasdasd'
    b = p.sub('',a)
    print b

if __name__ == '__main__':
    #change_dir_sources(__file__.replace('app.py',''))

    change_file(__file__.replace('app.py','data.txt'))

    